﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Library;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Model;
using System.Text;

namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeReceivablesYuanJianList : EasyUIPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "FTradeReceivablesYuanJianList";
            }
        }

        #region 属性 (查询条件)

        protected string ZDYongHu
        {
            get
            {
                return GetRequestValue<string>("ZDYongHu", "");

            }
        }

        protected string JingXiaoShang
        {
            get
            {
                return GetRequestValue<string>("JingXiaoShang", "");

            }
        }
        protected string HeTongHao
        {
            get
            {
                return GetRequestValue<string>("HeTongHao", "");

            }
        }
        protected string XingHao
        {
            get
            {
                return GetRequestValue<string>("XingHao", "");

            }
        }
        protected string JiHao
        {
            get
            {
                return GetRequestValue<string>("JiHao", "");

            }
        }

        protected string JiQiState
        {
            get
            {
                return GetRequestValue<string>("JiQiState", "-");
            }
        }
        protected string XiaoShouLeiXing
        {
            get
            {
                return GetRequestValue<string>("XiaoShouLeiXing", "");
            }

        }
        protected string YingYeJingLi
        {
            get
            {
                return GetRequestValue<string>("YingYeJingLi", "");
            }

        }
        protected string XiaoShouShiJianFrom
        {
            get
            {
                return GetRequestValue<string>("XiaoShouShiJianFrom", "");

            }
        }
        protected string XiaoShouShiJianTo
        {
            get
            {
                if (string.IsNullOrEmpty(XiaoShouShiJianFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("XiaoShouShiJianTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(XiaoShouShiJianFrom).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

            }
        }
        protected string FanHuanLiXiState
        {
            get
            {
                return GetRequestValue<string>("FanHuanLiXiState", "");

            }
        }
        protected string FanHuanDateFrom
        {
            get
            {
                return GetRequestValue<string>("FanHuanDateFrom", "");

            }
        }
        protected string FanHuanDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(FanHuanDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("FanHuanDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(FanHuanDateFrom).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

            }
        }

        protected string CreateDateFrom
        {
            get
            {
                DateTime dt = DateTime.Now; //当前时间
                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
                DateTime endWeek = startWeek.AddDays(6); //本周周日
                return startWeek.ToString("yyyy-MM-dd");
            }
        }
        protected string CreateDateTo
        {
            get
            {
                DateTime dt = DateTime.Now; //当前时间
                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
                DateTime endWeek = startWeek.AddDays(6); //本周周日
                return endWeek.ToString("yyyy-MM-dd");

            }
        }


        #endregion
        /// <summary>
        /// 排序字段 值为 id
        /// </summary>
        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "FTradeID" : base.orderField;
            }
        }
        /// <summary>
        ///  排序方式 desc或 asc
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "desc" : base.orderDirection;
            }
        }
        protected override string SQLSort
        {
            get
            {
                if (orderField == "JiHao")
                {
                    return orderField + " " + orderDirection;
                }
                return (orderField + " " + orderDirection + "  ");
            }
        }
        protected string GetSQLWhere()
        {
            //营业经理权限 2014-02-21
            string agentList = base.FTradeAgentPowerSqlWhere();
            string wheresql = string.IsNullOrEmpty(agentList) ? " 1=1 " : string.Format(" JingXiaoShang in ({0})", agentList);

            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(JingXiaoShang))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("JingXiaoShang", QueryOperatorsEnum.等于, JingXiaoShang);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            if (!string.IsNullOrEmpty(ZDYongHu))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("ZDYongHu", QueryOperatorsEnum.包含, ZDYongHu);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            if (!string.IsNullOrEmpty(HeTongHao))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("HeTongHao", QueryOperatorsEnum.等于, HeTongHao);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }
            if (!string.IsNullOrEmpty(YingYeJingLi))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("YingYeJingLi", QueryOperatorsEnum.等于, YingYeJingLi);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            if (!string.IsNullOrEmpty(XiaoShouShiJianFrom))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("XiaoShouShiJian", QueryOperatorsEnum.范围, XiaoShouShiJianFrom + "|" + XiaoShouShiJianTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            string dutySQL = "";
            if (!string.IsNullOrEmpty(XingHao))
            {
                dutySQL = QueryOperatorsEnumHelper.ConvertCommonOperator("XingHao", QueryOperatorsEnum.包含, XingHao);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + dutySQL;
                }
                else
                {
                    wheresql = dutySQL;
                }
            }
            dutySQL = "";
            if (!string.IsNullOrEmpty(JiHao))
            {
                dutySQL = QueryOperatorsEnumHelper.ConvertCommonOperator("JiHao", QueryOperatorsEnum.包含, JiHao);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + dutySQL;
                }
                else
                {
                    wheresql = dutySQL;
                }
            }

            if (JiQiState != "-")
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("JiQiState", QueryOperatorsEnum.等于, JiQiState);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }
            if (XiaoShouLeiXing != "")
            {
                string[] xslxs = XiaoShouLeiXing.Split(new char[] { ',' });
                string xslxsql = " XiaoShouLeiXing in (''";
                foreach (string itm in xslxs)
                {
                    if (!string.IsNullOrEmpty(itm))
                    {
                        xslxsql += string.Format(",'{0}'", itm);
                    }
                }
                xslxsql += ")";

                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + xslxsql;
                }
                else
                {
                    wheresql = xslxsql;
                }
            }

            if (FanHuanLiXiState != "")
            {
                string[] xslxs = FanHuanLiXiState.Split(new char[] { ',' });
                string xslxsql = "";// " FanHuanLiXiState in (";
                foreach (string itm in xslxs)
                {
                    if (!string.IsNullOrEmpty(itm))
                    {
                        xslxsql += string.Format(",'{0}'", itm);
                    }
                }
                //  xslxsql += ")";

                xslxsql = xslxsql.Substring(1, xslxsql.Length - 1);
                xslxsql = " FanHuanLiXiState in (" + xslxsql + ")";


                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + xslxsql;
                }
                else
                {
                    wheresql = xslxsql;
                }
            }

            //
            //if (FanHuanLiXiState != "")
            //{
            //    string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("FanHuanLiXiState", QueryOperatorsEnum.等于, FanHuanLiXiState);
            //    if (!string.IsNullOrEmpty(wheresql))
            //    {
            //        wheresql += " and " + DOWNSQL;
            //    }
            //    else
            //    {
            //        wheresql = DOWNSQL;
            //    }
            //}
            if (!string.IsNullOrEmpty(FanHuanDateFrom))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("FanHuanDate", QueryOperatorsEnum.范围, FanHuanDateFrom + "|" + FanHuanDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }
            string SQLHeTongYuanJian = string.Format("((HeTongYuanJian='未收到' ) or ( ShouHouYuanJian='未收到' )) ");
            if (!string.IsNullOrEmpty(wheresql))
            {
                wheresql += " and " + SQLHeTongYuanJian;
            }
            else
            {
                wheresql = SQLHeTongYuanJian;
            }


            return wheresql;
        }
        protected override string query()
        {

           // return "";
            DataTableCollection dts = FTradeReceivablesController.GetPagination(GetSQLWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);

            return ToDataGridJson(dts[0], TotalCount, null).ToString();
        }
        protected override string editrow()
        {
            //int FTradeID = GetRequestValue<int>("FTradeID", 0);
            //string HeTongYuanJian = GetRequestValue<decimal>("newgpsfeiyong", 0m);
            FTradeReceivablesInfo objTrade = Request.SerializableObject<FTradeReceivablesInfo>();
            bool XX = FTradeReceivablesController.UpdateYuanJian(objTrade);
            //if (NewFCatheringId == 0)
            //{
            //    SuccessJson("已成功调整GPS费用", this.DWZRef);
            //}
            //if (NewFCatheringId < 0)
            //{
            //    ErrorJson("调整GPS费用失败");
            //}

            return "";

        }
    }
}